<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" label-width="77px" class="ele-form-search" @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="举报内容编号" label-width="100px">
              <el-input :placeholder="'请输入举报内容编号'" v-model="where.id"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="举报内容类型:" label-width="100px">
              <el-select clearable v-model="where.type" placeholder="请选择举报内容类型" class="ele-fluid">
                <el-option v-for="(value, idx) in typeArr" :label="value" :value="idx" :key="idx" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="举报原因:">
              <el-select clearable v-model="where.report_type" placeholder="请选择举报原因" class="ele-fluid">
                <el-option v-for="(value) in reasonArr" :label="value.note" :value="value.id" :key="value.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="处理状态:">
              <el-select clearable v-model="where.status" placeholder="请选择举报处理状态" class="ele-fluid">
                <el-option v-for="(value, idx) in statusArr" :label="value" :value="idx" :key="idx" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="发帖设备:">
              <el-select clearable v-model="where.post_platform" placeholder="请选择发帖设备" class="ele-fluid">
                <el-option label="全部" value="" />
                <el-option label="IOS端" value="1" />
                <el-option label="安卓端" value="2" />
                <el-option label="PC端" value="3" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="8" :md="12">
            <el-form-item label="举报日期:">
              <el-date-picker style="width: 100%" v-model="where.time_range" type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss" label-format="yyyy-MM-dd HH:mm:ss" range-separator="至"
                start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openEdit(null)"
            v-if="permission.includes('sys:report:add')">添加
          </el-button>
          <el-button size="small" type="danger" icon="el-icon-delete" class="ele-btn-icon" @click="removeBatch"
            v-if="permission.includes('sys:report:dall')">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)"
            v-if="permission.includes('sys:report:edit')">查看详情
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除此举报记录吗？" @confirm="remove(row)">
            <el-link type="danger" slot="reference" :underline="false" icon="el-icon-delete"
              v-if="permission.includes('sys:report:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>

        <template slot="post_platform" slot-scope="{ row }">
          <el-button size="mini" v-if="row.post_platform == ''" type="info">全部</el-button>
          <el-button size="mini" v-if="row.post_platform == 1" type="success">IOS端</el-button>
          <el-button size="mini" v-if="row.post_platform == 2" type="primary">安卓端</el-button>
          <el-button size="mini" v-if="row.post_platform == 3" type="warning">PC端</el-button>
        </template>


      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <report-detail :data="current" :visible.sync="showEdit" :banRules="banRules" @done="reload" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ReportDetail from './report-detail';
import { statusArr, typeArr } from './report';
import axios from "axios";

export default {
  name: 'ExamBoxReport',
  components: { ReportDetail },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/boxReport/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'type',
          label: '举报内容类型',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return typeArr[cellValue];
          }
        },
        {
          prop: "post_platform",
          label: "举报设备",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 200,
          slot: "post_platform"
        },
        {
          prop: 'report_type',
          label: '举报原因',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          formatter: (row, column, cellValue) => {
            const foundItem = this.reasonArr.find(item => item.id === cellValue)
            if (!foundItem) {
              return '---'
            }
            return foundItem.note
          }
        },
        {
          prop: 'reason',
          label: '举报理由',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',

        },
        {
          prop: 'member_nickname',
          label: '举报人昵称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'member_id',
          label: '举报人编号',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'target_member_nickname',
          label: '被举报人昵称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'target_member_id',
          label: '被举报人id',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'status',
          label: '处理状态',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return statusArr[cellValue];
          }
        },

        // {
        //   prop: 'ban_rule_id',
        //   label: '处罚规则',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center',
        //   formatter: (row, column, cellValue) => {
        //     // console.log(this.banRules,cellValue)
        //     if (cellValue !== null) {
        //       const foundItem = this.banRules.find(item => item.id === cellValue);
        //       let str = '';
        //       if (foundItem.ban_member === '0'){
        //         str += '封号：永久'
        //       }else if(foundItem.ban_member && foundItem.ban_member !== '0') {
        //         str += '封号：'+ foundItem.ban_member+'天'
        //       }
        //       if (str) {
        //         str += '，'
        //       }
        //       if (foundItem.ban_comment === '0'){
        //         str += '限制评论：永久';
        //       }else if(foundItem.ban_comment && foundItem.ban_comment !== '0') {
        //         str += '限制评论：'+ foundItem.ban_comment+'天';
        //       }
        //       return str
        //     }
        //     return '';
        //   }
        // },
        {
          prop: 'ban_status',
          label: '处罚状态',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'create_time',
          label: '举报时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'deal_time',
          label: '操作时间',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'deal_username',
          label: '操作人',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        // {
        //   prop: 'update_time',
        //   label: '更新时间',
        //   sortable: 'custom',
        //   showOverflowTooltip: true,
        //   minWidth: 160,
        //   align: 'center',
        //   formatter: (row, column, cellValue) => {
        //     return this.$util.toDateString(cellValue);
        //   }
        // },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      statusArr,
      typeArr,
      reasonArr: [],
      banRules: [],
    };
  },
  created() {
    axios.post("/boxBanConfig/index").then((res) => {
      // console.log(res.data.data);
      this.reasonArr = res.data.data;
    });
    axios.post("/boxBanRule/index").then((res) => {
      // console.log(res.data.data);
      this.banRules = res.data.data;
    });
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      // if (row.ban_status) {
      //   this.$message.error('需等待用户上次处罚结束');
      //   return;
      // }
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http.post('/boxreport/delete', { id: row.id }).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的举报记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({ lock: true });
        this.$http.post('/boxreport/delete', { id: this.selection.map(d => d.id) }).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped></style>
